package top.mable.payment.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import top.mable.common.vo.DemoMsg;

import java.io.IOException;


/**
 * 注解使用 rabbitmq,绑定死信队列。即：消息被拒绝后进入死信
 * @Author wen
 * @Date 2021/7/6
 **/

@Component
@Slf4j
public class DemoListener {

    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(value = "demo-exchange",type = ExchangeTypes.TOPIC),
            value = @Queue(value = "demo-queue",durable = Exchange.TRUE,
                    arguments = {@Argument(name = "x-dead-letter-exchange",value = "dead-exchange"),
                        @Argument(name = "x-dead-letter-routing-key", value = "dead-key")}),
            key = "demo.#")})
    public void getMsg(Message message, Channel channel) throws IOException {
        DemoMsg unserialize = ObjectUtil.unserialize(message.getBody());
        log.info("消息消费，内容为{}",unserialize.toString());
        //throw new RuntimeException();
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
